package com.yjt.fresh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.yjt.fresh.R;
import com.yjt.fresh.adpater.AdpRvAtyCouponForPay;
import com.yjt.fresh.base.BaseSimpleActivity;
import com.yjt.fresh.bean.BeanStoreCoupon;
import com.yjt.fresh.constant.MyConstants;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by UPC on 2018/8/14.
 */

public class AtyCouponForPay extends BaseSimpleActivity {
    public static final int COUPON_RESULT_CODE = 1009;
    @BindView(R.id.back)
    RelativeLayout mBack;
    @BindView(R.id.rvAppAtyCouponForPay)
    RecyclerView mRv;
    @BindView(R.id.tvAppAtyCouponForPayComplete)
    TextView mTvComplete;
    private AdpRvAtyCouponForPay mAdpRvAtyCouponForPay;
    private BeanStoreCoupon mBeanStoreCoupon;
    private int mPosition;


    @Override
    protected int getContentView() {
        return R.layout.app_aty_coupon_for_pay;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
    }

    @Override
    protected void initEvent() {
        initRv();
        initClick();
        Bundle bundle = getIntent().getBundleExtra(MyConstants.COUPON_OBJ);
        ArrayList<BeanStoreCoupon> storeCoupons = bundle.getParcelableArrayList(MyConstants.COUPON_OBJ);
        for (int i = 0; i < storeCoupons.size(); i++) {
            if (storeCoupons.get(i).isCheck()){
                mPosition = i;
                mBeanStoreCoupon = storeCoupons.get(i);
            }
        }
        mAdpRvAtyCouponForPay.setNewData(storeCoupons);
    }

    private void initClick() {
        mAdpRvAtyCouponForPay.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                List<BeanStoreCoupon> list = mAdpRvAtyCouponForPay.getData();
                for (int i = 0; i < list.size(); i++) {
                    if (i == position){
                        if (list.get(i).isCheck()){
                            list.get(i).setCheck(false);
                            mBeanStoreCoupon = null;
                        }else {
                            list.get(i).setCheck(true);
                            mBeanStoreCoupon = list.get(position);
                            mPosition = position;
                        }
                    }else {
                        list.get(i).setCheck(false);
                    }
                }
                mAdpRvAtyCouponForPay.notifyDataSetChanged();

            }
        });

        mTvComplete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mBeanStoreCoupon != null) {
                    Intent intent = new Intent();
                    intent.putExtra(MyConstants.COUPON_OBJ,mBeanStoreCoupon);
                    intent.putExtra(MyConstants.COUPON_POSITION,mPosition);
                    setResult(COUPON_RESULT_CODE,intent);
                    finish();
                }else {
                    finish();
                }
            }
        });

        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @NonNull
    private void initRv() {
        List<BeanStoreCoupon> list = new ArrayList<>();
        mAdpRvAtyCouponForPay = new AdpRvAtyCouponForPay(R.layout.app_recycle_item_aty_mycoupon_frg, list);
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        mRv.setLayoutManager(manager);
        mRv.setAdapter(mAdpRvAtyCouponForPay);
        mAdpRvAtyCouponForPay.bindToRecyclerView(mRv);
        mAdpRvAtyCouponForPay.setEmptyView(R.layout.view_empty);

    }


}
